package live.pay;



import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import live.api.impl.PaySendReqRes;
import live.common.Format;
import live.common.GetSequence;
import live.common.Log4jXmlConfig;
import live.common.PostParam;
import live.cz.entity.Order;
import live.jipiao.entity.AirOrder;
import live.response.WebServicePay;

public class PayManager {
	WebServicePay wsp = new WebServicePay();
	Logger LOG = Log4jXmlConfig.getInstance().returnLogger(PayManager.class);
	PaySendReqRes psr = null;
	/*
	 * 发送请求(话费充值)
	 */
	public void sendMobileRequestMobiel(Order order, String service_id,String buyer_id,String seller_id) {
		Map<String, Object> map = new TreeMap<String, Object>();
		map.put("service_id", service_id);
		// 发起方机构编码
		map.put("req_org_cd", PostParam.REQ_ORG_CD);
		// 发起时间
		map.put("req_trans_tm", Format.getInstance().dateFormatString(
				new Date(), "yyyyMMddHHmmss"));
		// 订单号
		map.put("req_trans_seq", GetSequence.returnSeqNum());
		map.put("order_id", PostParam.REQ_ORG_CD);
		map.put("seller_id", seller_id);
		map.put("sub_order_flag", "N");
		map.put("sub_order_num", "0");
		map.put("sub_order_dtl", "");
		map.put("order_desc", mobileOrderDesc(order));
		map.put("tot_trans_amt", String
				.valueOf((int) (order.getSalePrice() * 100)));
		map.put("buyer_id", buyer_id);
		map.put("valid_date", "0");
		map.put("notify_url", PostParam.MOBILE_NOTIFY);
		map.put("return_url", "");
		map.put("sign", SignUtil.sign(map, PostParam.PAYKEY));
		map.put("sign_type", PostParam.SIGN_TYPE);
		try {
			psr = new PaySendReqRes();
			psr.insertSendRequest(map);
			wsp.pixNull(PostParam.PAY_API, map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			LOG.error("话费充值发送服务【"+service_id+"】出现错误\n");
			LOG.error(new StringBuffer("订单参数：").append(JSONObject.fromObject(order).toString()).append(";服务编码：").append(service_id).append("\n").toString(),e);
		}
	}

	/*
	 * 发送请求(机票预订)
	 */
	public void sendFlightRequestMobiel(AirOrder order, String service_id,String buyer_id) {
		Map<String, Object> map = new TreeMap<String, Object>();
		map.put("service_id", service_id);
		// 发起方机构编码
		map.put("req_org_cd", PostParam.REQ_ORG_CD);
		// 发起时间
		map.put("req_trans_tm", Format.getInstance().dateFormatString(
				new Date(), "yyyyMMddHHmmss"));
		// 订单号
		map.put("req_trans_seq", GetSequence.returnSeqNum());
		map.put("order_id", order.getOrderNo());
		map.put("seller_id", "sansheng");
		map.put("sub_order_flag", "N");
		map.put("sub_order_num", "0");
		map.put("sub_order_dtl", "");
		map.put("order_desc", flightOrderDesc(order));
		map.put("tot_trans_amt", String.valueOf((int) (Double.parseDouble(order
				.getOrderTotal().toString()) * 100)));
		map.put("buyer_id", buyer_id);
		map.put("valid_date", "0");
		map.put("notify_url", PostParam.MOBILE_NOTIFY);
		map.put("return_url", "");
		map.put("sign", SignUtil.sign(map, PostParam.PAYKEY));
		map.put("sign_type", PostParam.SIGN_TYPE);
		try {
			psr = new PaySendReqRes();
			psr.insertSendRequest(map);
			wsp.pixNull(PostParam.PAY_API.toString(), map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			LOG.error("机票预订【"+service_id+"】出现错误\n");
			LOG.error(new StringBuffer("订单参数：").append(JSONObject.fromObject(order).toString()).append(";服务编码：").append(service_id).append("\n").toString(),e);
		}
	}

	/**
	 * 单笔对账
	 * @param params
	 */
	public void reconOrder(Map params) {
		Map<String, Object> map = new TreeMap<String, Object>();
		map.put("service_id", ServiceIds.SINGLE_PEN_RECON);
		map.put("req_org_cd", PostParam.REQ_ORG_CD);
		map.put("req_trans_tm", Format.getInstance().dateFormatString(
				new Date(), "yyyyMMddHHmmss"));
		map.put("req_trans_seq", GetSequence.returnSeqNum());
		map.put("original_service_id", params.get("service_id"));
		map.put("order_id", params.get("order_id"));
		map.put("original_trans_seq", params.get("seqnum"));
		if (params.get("type").equals("mobile")) {
			map.put("notify_url", PostParam.MOBILE_NOTIFY);
		} else {
			map.put("notify_url", PostParam.FLIGHT_NOTIFY);
		}
		map.put("sign", SignUtil.sign(map, PostParam.PAYKEY));
		map.put("sign_type", PostParam.SIGN_TYPE);
		try {
			wsp.pixNull(PostParam.PAY_API.toString(), map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 话费充值描述
	 * 
	 * @param order
	 * @return
	 */
	public String mobileOrderDesc(Order order) {
		String result = new StringBuffer(order.getProdInfo()).append(
				order.getProPrice()).append("元充值[").append(order.getPhonenum())
				.append("]").append((int) (order.getSalePrice() * 100))
				.toString();
		return result;
	}

	/**
	 * 机票预订描述
	 * 
	 * @param order
	 * @return
	 */
	public String flightOrderDesc(AirOrder order){
		String result=new StringBuffer("机票预订[").append(order.getOrderNo()).append("];")
		.append(Format.getInstance().returnFormatString(order.getSdate().toString(), "yyyy-MM-dd"))
		.append(";").append(order.getScity().toString()).append("-").append(order.getEcity().toString())
		.toString();
		return result;
	}

}
